
#include "NewBoost.Data.h"

using namespace new_boost;

// [*] CNewBoostGroundTruthLabel

bool new_boost::CNewBoostGroundTruthLabel::operator<( const CNewBoostGroundTruthLabel& Another ) const
{
	if (image_index != Another.image_index) return image_index < Another.image_index;
	if (object_index != Another.object_index) return object_index < Another.object_index;
	return false;
}

// [*] CNewBoostSrcImageList

void new_boost::CNewBoostSrcImageList::myInit(
	int BicFileNameSetIndex,
	string strPN_SrcImage,
	int Amount_SrcImage,
	string strPN_SrcAnnotation,
	string strFN_GT_LabelList)
{
	this->BicFileNameSetIndex = BicFileNameSetIndex;
	this->strPN_SrcImage = strPN_SrcImage;
	this->Amount_SrcImage = Amount_SrcImage;
	this->strPN_SrcAnnotation = strPN_SrcAnnotation;
	this->strFN_GT_LabelList = strFN_GT_LabelList;

	PerImagePosAmount.Resize(Amount_SrcImage);
	PerImagePosAmount.Fill(0);
	GT_LabelList.Clear();

	if (strFN_GT_LabelList == "") return;

	int i;
	ifstream inFile(strFN_GT_LabelList.c_str());
	inFile>>Amount_GT_Total;
	GT_LabelList.Resize(Amount_GT_Total);
	Amount_GT_Easy = 0;
	Amount_GT_Hard = 0;
	Amount_GT_Unknown = 0;
	for ( i = 0; i < Amount_GT_Total; ++i )
	{
		CNewBoostGroundTruthLabel& gtl = GT_LabelList.pElement[i];
		inFile>>gtl.image_index;
		inFile>>gtl.object_index;
		inFile>>gtl.box.x;
		inFile>>gtl.box.y;
		inFile>>gtl.box.w;
		inFile>>gtl.box.h;
		inFile>>gtl.difficulty;

		if (gtl.difficulty == 1)
			++Amount_GT_Easy;
		else if (gtl.difficulty == 2)
			++Amount_GT_Hard;
		else
			++Amount_GT_Unknown;
	}
	sort(GT_LabelList.Begin(), GT_LabelList.End());
	inFile.close();
	inFile.clear();
}

std::string new_boost::CNewBoostSrcImageList::GetFN_SrcImage( int ImageIndex ) const
{
	char strTemp[10001];
	sprintf_s(strTemp, "%s%04d.jpg", strPN_SrcImage.c_str(), ImageIndex);
	return strTemp;
}

std::string new_boost::CNewBoostSrcImageList::GetFN_SrcAnnotation( int ImageIndex ) const
{
	char strTemp[10001];
	sprintf_s(strTemp, "%s%04d.jpg", strPN_SrcAnnotation.c_str(), ImageIndex);
	return strTemp;
}

void new_boost::CNewBoostSrcImageList::MakeBicFileNameSet_SrcImage( CBicExampleParentFileNameSet& DstBicFileNameSet ) const
{
	int i;
	DstBicFileNameSet.SetIndex = BicFileNameSetIndex;
	DstBicFileNameSet.FileNameList.Clear();
	for ( i = 0; i < Amount_SrcImage; ++i )
		DstBicFileNameSet.FileNameList.PushBack(this->GetFN_SrcImage(i));
}

// [*] CNewBoostCropGT_SamplingSettings

void new_boost::CNewBoostCropGT_SamplingSettings::myInit(
	int IdealAmount,
	double IdealRatio_Easy,
	double IdealRatio_Hard,
	double IdealRatio_Unknown)
{
	this->IdealAmount = IdealAmount;
	this->IdealRatio_Easy = IdealRatio_Easy;
	this->IdealRatio_Hard = IdealRatio_Hard;
	this->IdealRatio_Unknown = IdealRatio_Unknown;
}

void new_boost::CNewBoostCropGT_SamplingSettings::GetTargetExampleAmount(
	int& DstTargetExampleAmount_Easy,
	int& DstTargetExampleAmount_Hard,
	int& DstTargetExampleAmount_Unknown,
	const CNewBoostSrcImageList& SrcSIL ) const
{
	int AE = SrcSIL.Amount_GT_Easy * 2;
	int AH = SrcSIL.Amount_GT_Hard * 2;
	int AU = SrcSIL.Amount_GT_Unknown * 2;

	DstTargetExampleAmount_Easy = AE;
	DstTargetExampleAmount_Hard = AH;
	DstTargetExampleAmount_Unknown = AU;

	if (IdealAmount < 0)
	{
		if (IdealRatio_Easy>-EPS && IdealRatio_Easy<1.0)
			DstTargetExampleAmount_Easy = CMyMath::TranslateNumber_Round(AE * IdealRatio_Easy, (int)0);
		if (IdealRatio_Hard>-EPS && IdealRatio_Hard<1.0)
			DstTargetExampleAmount_Hard = CMyMath::TranslateNumber_Round(AH * IdealRatio_Hard, (int)0);
		if (IdealRatio_Unknown>-EPS && IdealRatio_Unknown<1.0)
			DstTargetExampleAmount_Unknown = CMyMath::TranslateNumber_Round(AU * IdealRatio_Unknown, (int)0);
		return;
	}

	double ss = 0.0;
	int tt = IdealAmount;
	bool f_Easy, f_Hard, f_Unknown;

	f_Easy = (IdealRatio_Easy>-EPS && AE>0);
	if (f_Easy) ss += IdealRatio_Easy; else tt -= AE;
	f_Hard = (IdealRatio_Hard>-EPS && AH>0);
	if (f_Hard) ss += IdealRatio_Hard; else tt -= AH;
	f_Unknown = (IdealRatio_Unknown>-EPS && AU>0);
	if (f_Unknown) ss += IdealRatio_Unknown; else tt -= AU;

	if (f_Easy)
	{
		DstTargetExampleAmount_Easy = CMyMath::TranslateNumber_Round(tt * IdealRatio_Easy / ss, (int)0);
		if (DstTargetExampleAmount_Easy < 0) DstTargetExampleAmount_Easy = 0;
		if (DstTargetExampleAmount_Easy > AE) DstTargetExampleAmount_Easy = AE;
		ss -= IdealRatio_Easy;
		tt -= DstTargetExampleAmount_Easy;
	}

	if (f_Hard)
	{
		DstTargetExampleAmount_Hard = CMyMath::TranslateNumber_Round(tt * IdealRatio_Hard / ss, (int)0);
		if (DstTargetExampleAmount_Hard < 0) DstTargetExampleAmount_Hard = 0;
		if (DstTargetExampleAmount_Hard > AH) DstTargetExampleAmount_Hard = AH;
		ss -= IdealRatio_Hard;
		tt -= DstTargetExampleAmount_Hard;
	}

	if (f_Unknown)
	{
		DstTargetExampleAmount_Unknown = CMyMath::TranslateNumber_Round(tt * IdealRatio_Unknown / ss, (int)0);
		if (DstTargetExampleAmount_Unknown < 0) DstTargetExampleAmount_Unknown = 0;
		if (DstTargetExampleAmount_Unknown > AU) DstTargetExampleAmount_Unknown = AU;
		ss -= IdealRatio_Unknown;
		tt -= DstTargetExampleAmount_Unknown;
	}
}

